package weaver.formmode.customjavacode.modeexpand.hitech.utils;

import weaver.conn.RecordSet;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.TimeUtil;
import weaver.general.Util;

import java.util.*;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2022-09-07
 * @Description: ${description}
 * @ModifyLog:
 **/

public class HtModelUtil {

    //将数据插入表单建模
    public static int insertModelData(int modeid, int userid, Map map) {
        int dataid = 0;
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        //查询该模块表名
        String sql = "select b.tablename,a.formid from modeinfo a left join workflow_bill b on a.formid=b.id where a.id=?";
        rs.executeQuery( sql, modeid );
        rs.next();
        String tablename = Util.null2String( rs.getString( "tablename" ) );
        String uuid = UUID.randomUUID().toString();
        String insSql = "insert into " + tablename
                + "(uuid,modedatacreater,modedatacreatedate,modedatacreatetime,formmodeid)values(?,?,?,?,?)";
        Object[] params = {uuid, userid, TimeUtil.getCurrentDateString(), TimeUtil.getOnlyCurrentTimeString(), modeid};
        if (rs.executeUpdate( insSql, params )) {
            rs1.executeQuery( "select id from " + tablename + " where uuid=?", uuid );
            rs1.next();
            dataid = Util.getIntValue( rs1.getString( "id" ) );
            if (dataid > 0) {
                //遍历数据 进行update
                String updatesql = "update " + tablename + " set ";
                Set<String> keySet = map.keySet();
                for (String key : keySet) {
                    updatesql += key + "='" + map.get( key ).toString() + "',";
                }
                if (updatesql.endsWith( "," )) {
                    updatesql = updatesql.substring( 0, updatesql.length() - 1 );
                    updatesql += " where id=" + dataid;
                    rs.executeUpdate( updatesql );
                }
                //进行权限重构
                getNewModeRight( userid, modeid, dataid );
            }
        }
        return dataid;
    }

    //建模权限重构
    public static void getNewModeRight(int userid, int formmodeid, int id) {
        ModeRightInfo modeRightInfo = new ModeRightInfo();
        modeRightInfo.setNewRight( true );
        modeRightInfo.editModeDataShare( userid, formmodeid, id );
    }

    public static Map<String, String> getOrders(String orderid) {
        RecordSet rs = new RecordSet();
        Map<String, String> map = new HashMap<>();
        rs.executeQuery( "select AUFNR,GSBER,KTEXT,USER6 from uf_order where id=?", orderid );
        if (rs.next()) {
            map.put( "AUFNR", Util.null2String( rs.getString( "AUFNR" ) ) );
            map.put( "GSBER", Util.null2String( rs.getString( "GSBER" ) ) );
            map.put( "KTEXT", Util.null2String( rs.getString( "KTEXT" ) ) );
            map.put( "USER6", Util.null2String( rs.getString( "USER6" ) ) );
        }
        return map;
    }

    public static List<Map<String, String>> getKsByOrder(String code, String ks){
        RecordSet rs = new RecordSet(  );
        List<Map<String, String>> mapList = new ArrayList<>(  );
        rs.executeQuery("select b.id,b.departmentcode,b.departmentname,b.supdepid from HrmDepartment a " +
                "join HrmDepartment b on a.id=b.supdepid where " +
                "a.departmentcode ='"+code+"' and b.id in ("+ks+")");
        while (rs.next()){
            Map<String, String> map = new HashMap<>();
            map.put("ksid",Util.null2String( rs.getString( "id" ) ));
            map.put("departmentcode",Util.null2String( rs.getString( "departmentcode" ) ));
            map.put("departmentname",Util.null2String( rs.getString( "departmentname" ) ));
            map.put("supdepid",Util.null2String( rs.getString( "supdepid" ) ));
            mapList.add( map );
        }
        return mapList;
    }

}
